package trabajoISW2.domain.implementations;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import javax.persistence.ElementCollection;
import javax.persistence.Embeddable;

import trabajoISW2.domain.contracts.ICart;
import trabajoISW2.domain.exceptions.NoPositiveNumberException;

@Embeddable
public class Cart implements ICart {

	@ElementCollection(targetClass = Detail.class)
	private List<Detail> listDetail;
	private double amount = 0;

	public Cart(List<Detail> listDetail) {

		this.listDetail = listDetail;
		this.amount = calculateAmount(listDetail);

	}

	public Cart() {
		// TODO Auto-generated constructor stub
		listDetail = new LinkedList<Detail>();
	}

	// Devuelve true si se ha eliminado correctamente el detalle y false si no
	// se elimina
	@Override
	public boolean removeDetail(int detailId) {
		if (detailId < 0 | detailId > Integer.MAX_VALUE)
			throw new NoPositiveNumberException();
		else {
			boolean resultado = false;
			Detail d = getDetail(detailId);
			if (d != null) {
				resultado = true;
				this.amount -= d.getBook().getPrice() * d.getQuantity();
				resultado = listDetail.remove(d);
			}
			return resultado;
		}
	}

	// comprobado

	@Override
	public boolean modifyDetail(int itemId, int quantity) {
		if (itemId < 0 | itemId > Integer.MAX_VALUE | quantity < 0
				| quantity > 99)
			throw new IllegalArgumentException();

		else {
			boolean resultado = false;
			Detail d = getDetail(itemId);
			if (d != null) {
				resultado = true;
				this.amount -= d.getQuantity() * d.getBook().getPrice();
				d.setQuantity(quantity);
				this.amount += d.getQuantity() * d.getBook().getPrice();
			}
			return resultado;
		}
	}

	// comprobado

	@Override
	public Detail getDetail(int detailId) {
		Detail resultado = null;
		Detail d = null;

		Iterator<Detail> it = this.listDetail.iterator();
		while (it.hasNext()) {
			d = (Detail) it.next();
			if (d.getDetailId() == detailId)
				resultado = d;
		}
		return resultado;
	}

	@Override
	public void setDetails(List<Detail> details) {
		if (details == null)
			throw new IllegalArgumentException();
		else {
			listDetail = details;
			amount = calculateAmount(details);
		}
	}

	@Override
	public boolean addDetail(Detail detail) {
		if (detail == null)
			throw new IllegalArgumentException();
		else {
			listDetail.add(detail);
			amount += detail.getQuantity() * detail.getBook().getPrice();
			return true;

		}

	}

	@Override
	public List<Detail> getDetails() {
		return listDetail;
	}

	// terminar de hacer
	@Override
	public String toString() {
		String s = "Productos: \n" + getDetails();
		return s;
	}

	public double calculateAmount(List<Detail> details) {
		Iterator<Detail> it = details.iterator();
		Detail d;
		double res = 0;
		while (it.hasNext()) {
			d = it.next();
			res += d.getBook().getPrice() * d.getQuantity();
		}
		return res;

	}

	@Override
	public double getAmount() {
		// TODO Auto-generated method stub
		return amount;
	}
	
	public boolean equals (Object o){
		boolean b = false;
		Cart c = (Cart) o;
		if (getDetails().containsAll(c.getDetails()))
			b = true;
		return b;
	}

}